package registnet.base.source;

import org.apache.torque.TorqueException;

import registnet.base.courseoffering.CourseOffering;

/**
 * Defines a class for creating a <code>CourseOffering</code> from the data from the database.
 */
public class CourseOfferingSource implements ICourseOfferingSource{

	/**
	 * The course offering
	 */
	private CourseOffering courseOffering;
	/**
	 * The course offering id of the course offering from the database.
	 */
	private Integer courseOfferingId;
	
	/**
	 * Creates a new course offering from the course offering from the database with the given course offering id.
	 * @param courseOfferingId
	 */
	public CourseOfferingSource(Integer courseOfferingId){
		this.courseOfferingId = courseOfferingId;
		load();
	}
	
	@Override
	public CourseOffering getCourseOffering() {
		return courseOffering;
	}

	@Override
	public CourseOffering load() {
		registnet.dao.CourseOffering daoCourseOffering = new registnet.dao.CourseOfferingPeer().getCourseOffering(courseOfferingId);
		
		if (courseOffering != null) return courseOffering;
		else courseOffering = new CourseOffering();
		
		try {
			//courseCode
			courseOffering.setCourseCode(daoCourseOffering.getCourse().getCourseCode());
			
			//name
			courseOffering.setName(daoCourseOffering.getCourseOfferingName());
			
			//studentCount
			courseOffering.setStudentCount(daoCourseOffering.getStudentCount());
			
			//laboratoryDay
			courseOffering.setLaboratoryDay(daoCourseOffering.getLaboratoryDay());
			
			//lectureDay
			courseOffering.setLectureDay(daoCourseOffering.getLectureDay());
			
			//recitationDay
			courseOffering.setRecitationDay(daoCourseOffering.getRecitationDay());
			
			//laboratoryTime
			courseOffering.setLaboratoryTime(daoCourseOffering.getLaboratoryTime());
			
			//lectureTime
			courseOffering.setLectureTime(daoCourseOffering.getLectureTime());
			
			//recitationTime
			courseOffering.setRecitationTime(daoCourseOffering.getRecitationTime());
			
			//laboratoryRoom
			courseOffering.setLaboratoryRoom(daoCourseOffering.getLaboratoryRoom());
			
			//lectureRoom
			courseOffering.setLectureRoom(daoCourseOffering.getLectureRoom());
			
			//recitationRoom
			courseOffering.setRecitationRoom(daoCourseOffering.getRecitationRoom());
			
			//isTeamTeach
			if(daoCourseOffering.getIsTeamTeach() == 0) 
				courseOffering.setTeamTeach(false);
			else
				courseOffering.setTeamTeach(true);
			
			//laboratoryHours
			courseOffering.setLaboratoryHours(daoCourseOffering.getLaboratoryHours());
			
			//lectureHours
			courseOffering.setLectureHours(daoCourseOffering.getLectureHours());
			
			//recitationHours
			courseOffering.setRecitationHours(daoCourseOffering.getRecitationHours());
			
		} catch (TorqueException e) {
			e.printStackTrace();
		}
		
		return courseOffering;
	}

}
